package com.ecomemerce.app.comment.widgets

import android.util.Log
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.sp

@Composable
@Stable
fun PriceText(price: String) {
    val annotatedString = buildAnnotatedString {
        // 解析价格字符串，例如 "AED 78.20"
        val regex = Regex("([A-Za-z]+)?\\s*(\\d+)(\\.\\d+)?\\s*([A-Za-z]+)?")
        val matchResult = regex.find(price)
        Log.e("matchResult",price)
        matchResult?.let { match ->
            Log.e("matchResult",match.toString())
            val currencyPrefix = match.groupValues[1]  // "AED"
            val integerPart = match.groupValues[2] // "78"
            val decimalPart = match.groupValues[3] // ".20"
            val currencySuffix = match.groupValues[4]?.trim() // 可能是 "AED"
            // 确定货币单位位置
            val currency = currencyPrefix ?: currencySuffix ?: ""
            // 货币符号
            withStyle(style = SpanStyle(fontSize = 8.sp, color = Color.Red)) {
                append(currency + " ")
            }
            // 主要价格部分（放大）
            withStyle(style = SpanStyle(fontSize = 12.sp, fontWeight = FontWeight.Bold, color = Color.Red)) {
                append(integerPart)
            }
            // 小数部分
            decimalPart?.let {
                withStyle(style = SpanStyle(fontSize = 8.sp, color = Color.Red)) {
                    append(it)
                }
            }
        } ?: append(price) // 如果正则解析失败，直接添加原始文本
    }

    Text(
        text = annotatedString,
        style = TextStyle(lineHeight = 10.sp) // 控制行高
    )
}